import { Component , OnInit , Injector } from '@angular/core';

import { RoleServiceProxy, RoleListDto } from '@shared/service-proxies/service-proxies';

import { MessageService } from '@core/net/message/MessageService';
import { AppComponentBase } from '@shared/app-component-base';

import { CreateOrEditRoleModalComponent } from './create-or-edit-role-modal.component';

@Component({
    selector: 'app-role',
    templateUrl: './role.component.html'
})
export class RoleComponent extends AppComponentBase implements OnInit {

    pi = 1;
    ps = 10;
    total = 200; // mock total
    list = [];
    loading = false;
    args: any = { };
    _indeterminate = false;
    _allChecked = false;

    constructor(
        injector: Injector, 
        private _roleService: RoleServiceProxy,
        private _messageService: MessageService
    ) { 
        super(injector);
    }

    toggleFull() {
    }

    load(pi?: number) {
        if (typeof pi !== 'undefined') {
            this.pi = pi || 1;
        }

        this.loading = true;
        // this._allChecked = false;
        this._indeterminate = false;

        this._roleService.getRoles(undefined)
        .finally(() => {
            this.loading = false;
        })
        .subscribe(result => {
            this.list = result.items;
            this.total = result.items.length;

        });

    }

    deleteRole(role: RoleListDto): void {
        this.message.confirm(
            this.l('RoleDeleteWarningMessage', role.displayName),
            isConfirmed => {
                if (isConfirmed) {
                    this._roleService.deleteRole(role.id).subscribe(() => {
                        this.load();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                    });
                }
            }
        );
    }


    ngOnInit() {
        this.load();
    }


    createRole(id?: number): void {
        this.showModal(CreateOrEditRoleModalComponent,{
            id : id
        }, () => {
            this.load();
        });
    }


}
